package com.xiu.travel.util;

import com.xiu.travel.security.pojo.SecurityUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 权限获取工具类
 *
 * @author ruoyi
 */
public class SecurityUtils
{

    /**
     * 获取用户ID
     */
    public static Long getUserId()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication.getPrincipal() instanceof String)
        {
            return 0L;
        }
        else
        {
            SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
            return securityUser.getId();
        }

    }
//    /**
//     * 获取Token
//     */
//    public static String getToken()
//    {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//            SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
//            return securityUser.getToken();
//
//    }

    /**
     * 获取用户名称
     */
    public static String getUsername()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
        return securityUser.getUsername();
    }

    /**
     * 获取用户key
     */
    public static String getUserKey()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
        return "login:" + securityUser.getId() + "";
    }


    /**
     * 获取登录用户信息
     */
    public static SecurityUser getLoginUser()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (SecurityUser) authentication.getPrincipal();
    }

    /**
     * 获取登录用户权限信息
     */
    public static List<String> getPermissions()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
        return securityUser.getPermissions();
    }

    /**
     * 获取登录用户权限信息
     */
    public static List<String> getRole()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
        return securityUser.getRoles();
    }

    /**
     * 获取请求token
     */
    public static String getToken()
    {
        return getToken(Objects.requireNonNull(ServletUtils.getRequest()));
    }

    /**
     * 根据request获取请求token
     */
    public static String getToken(HttpServletRequest request)
    {
        // 从header获取token标识
        String token = request.getHeader("token");
        return token;
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
}
